package com.ebt.m.customer.util;

import android.text.TextUtils;

import com.ebt.m.customer.cache.CustomerGeneralCache;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.db.CustomerInteraction;
import com.ebt.m.customer.entity.CustomerAtta;
import com.ebt.m.customer.entity.CustomerContact;
import com.ebt.m.customer.entity.CustomerDetail;
import com.ebt.m.customer.entity.CustomerSimple;
import com.ebt.m.customer.model.CustomerModel;
import com.ebt.m.customer.model.CustomerModelDetail;
import com.ebt.m.customer.model.CustomerModelNew;
import com.ebt.m.customer.net.json.CustomerTapeJson;
import com.ebt.m.data.bean.BeanCustomerTape;
import com.ebt.m.data.entity.Customer;
import com.ebt.m.data.entity.EntityCustomerAttachment;
import com.ebt.m.utils.DateUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class CustomerConvertUtil {

    public static List<CustomerModelNew> convertCustomerListToCustomerModelNewList(List<CustomerSimple> inList) {
        List<CustomerModelNew> outList = new ArrayList<>();
        for (CustomerSimple item : inList) {
            String sortKeyString = PinyinUtil.generateDisplayStringSingle(item.name);
            if (sortKeyString == null || sortKeyString.length() == 0) {
                outList.add(new CustomerModelNew(item.customerId, item.portraitPath, item.name, "#",
                        sortKeyString, item.tag, item.sex));
            } else {
                outList.add(new CustomerModelNew(item.customerId, item.portraitPath, item.name, String.valueOf(sortKeyString.charAt(0)).toUpperCase(),
                        sortKeyString, item.tag, item.sex));
            }
        }
        Collections.sort(outList, CompareHelper.COMPARATOR_CUSTOMER_MODEL_NEW);
        return outList;
    }

    public static List<CustomerSimple> order(List<CustomerSimple> inList) {
        for (CustomerSimple item : inList) {
            String sortKeyString = PinyinUtil.generateDisplayStringSingle(item.name);
            if (sortKeyString == null || sortKeyString.length() == 0) {
                item.setSortKeyChar("#");
                item.setSortKeyString(sortKeyString);
            } else {
                item.setSortKeyChar(String.valueOf(PinyinUtil.getSortKeyChar(sortKeyString)));
                item.setSortKeyString(sortKeyString);
            }
        }
        Collections.sort(inList, CompareHelper.COMPARATOR_CUSTOMER_SIMPLE);
        return inList;
    }

    /**
     * 转换
     *
     * @param item
     * @return
     */
    public static CustomerModelNew convertCustomerActiveToCustomerModelNew(CustomerSimple item) {
        String sortKeyString = PinyinUtil.generateDisplayStringSingle(item.name);
        return new CustomerModelNew(item.customerId, item.portraitPath, item.name, "#",
                sortKeyString, item.tag, item.sex);
    }

    public static BeanCustomerTape convertCustomerTapeJson2BeanCustomerTape(CustomerTapeJson json) {
        if (json == null) {
            return null;
        }
        BeanCustomerTape tape = new BeanCustomerTape();
        if (json.createTime == null) {
            json.createTime = DateUtils.getCurrentTimestampLong();
        }
        tape.setCreateTime(new Date(json.createTime));
        tape.setFilePath(json.filePath);
        tape.setNoteUuid(json.noteId);
        tape.setServerId(json.serverId);
        tape.setUuid(json.uuid);
        if (json.tapeDuration != null) {
            tape.setTapeDuration(json.tapeDuration.intValue() / 1000 + 1);
        } else {
            tape.setTapeDuration(0);
        }
        return tape;
    }





    public static CustomerModelNew convertCustomerDetailToCustomerModelNew(CustomerDetail customerDetail) {
        if (customerDetail == null || TextUtils.isEmpty(customerDetail.getName())) {
            return null;
        }

        String sortKeyString = PinyinUtil.generateDisplayStringSingle(customerDetail.getName());
        CustomerModelNew customerModelNew = new CustomerModelNew(customerDetail.getCustomerId(),
                customerDetail.getPortraitPath(), customerDetail.getName(),
                String.valueOf(sortKeyString.charAt(0)).toUpperCase(),
                sortKeyString, customerDetail.getTag(), customerDetail.getSex());
        return customerModelNew;
    }

    public static Customer convertCustomerDetailToCustomer(CustomerDetail customerDetail) {
        if (customerDetail == null) {
            return null;
        }

        Customer customer = new Customer();
        customer.setUuid(customerDetail.getCustomerId());
        customer.setName(customerDetail.getName());
        customer.setPortraitPath(customerDetail.getPortraitPath());
        customer.setSex(customerDetail.getSex());
        customer.setEducationGrading(String.valueOf(customerDetail.getEducationGrading()));
        customer.setBirthday(new Date(DateUtils.parseTimestampInSecond(customerDetail.getBirthday())));
        customer.setCareerCategory(customerDetail.getCareerCategory());
        customer.setIsConfirm(customerDetail.getIsConfirm());
        customer.setIsMarrage(customerDetail.getMarriage());
        customer.setNamePrefix(customerDetail.getNamePrefix());
        customer.setTag(customerDetail.getTag());
        customer.setLevel(customerDetail.getLevel());

        List<CustomerContact> listContact = customerDetail.getListCustomerContact();
        if (listContact != null) {
            for (CustomerContact item : listContact) {
                if (TextUtils.isEmpty(customer.getCellPhone())
                        && item != null
                        && CustomerConstant.CustomerContact.TYPE_PHONE == item.ctype) {
                    customer.setCellPhone(item.cvalue);
                }
                if (TextUtils.isEmpty(customer.getEmail())
                        && item != null
                        && CustomerConstant.CustomerContact.TYPE_EMAIL == item.ctype) {
                    customer.setEmail(item.cvalue);
                }
            }
        }

        return customer;
    }


    public static List<CustomerModel> convertListCustomerToCustomerModelGeneral(List<Customer> customers) {
        if (customers == null) {
            return null;
        }
        List<CustomerModel> list = new ArrayList<CustomerModel>();

        CustomerGeneralCache cache = CustomerGeneralCache.getInstance();
        cache.clearRemedySortKeys();

        for (Customer customer : customers) {
            String sortKeyString = customer.getSortKeyString();
            boolean flagNeedRemedy = false;
            if (TextUtils.isEmpty(sortKeyString)) {
                sortKeyString = PinyinUtil.generateDisplayStringSingle(customer.getName());
                flagNeedRemedy = true;
            }

            char sortKeyChar = getSortKeyChar(sortKeyString);
            if (flagNeedRemedy) {
                cache.addRemedySortKey(new SortKey(customer.getUuid(), sortKeyString, sortKeyChar));
            }
            list.add(new CustomerModel(customer.getUuid(),
                    customer.getName(),
                    sortKeyString,
                    sortKeyChar,
                    customer.getImportContact(),
                    Long.valueOf(customer.getLastActiveTime2() == null ? 0 : customer.getLastActiveTime2()),
                    customer.getPortraitPath()));
        }
        Collections.sort(list, CompareHelper.COMPARATOR_CUSTOMER_MODEL);
        return list;
    }


    public static List<CustomerModelNew> convertListCustomerToCustomerModelNew(List<Customer> customers) {
        if (customers == null) {
            return null;
        }
        List<CustomerModelNew> list = new ArrayList<>();

        for (Customer customer : customers) {

            String sortKeyString = customer.getSortKeyString();
            char sortKeyChar = getSortKeyChar(sortKeyString);
            list.add(new CustomerModelNew(customer.getUuid(),
                    customer.getPortraitPath(),
                    customer.getName(),
                    String.valueOf(sortKeyChar),
                    sortKeyString,
                    customer.getTag(),
                    customer.getSex()));
        }
        Collections.sort(list, CompareHelper.COMPARATOR_CUSTOMER_MODEL_NEW);
        return list;
    }

    public static List<CustomerModelDetail> convertListCustomerToCustomerModelDetail(List<Customer> customers, int type) {
        if (customers == null) {
            return null;
        }
        List<CustomerModelDetail> list = new ArrayList<CustomerModelDetail>();

        for (Customer customer : customers) {

            CustomerModelDetail item = new CustomerModelDetail(customer.getCustomerUuid(),
                    customer.getName(),
                    customer.getPortraitPath(),
                    false,//wechat
                    0,//score
                    0,//newsNumber
                    customer.getLastActiveTime2(),//TODO 暂时没有用到
                    type);
            list.add(item);
        }
        return list;
    }

    public static char getSortKeyChar(String sortKeyString) {
        if (!TextUtils.isEmpty(sortKeyString)) {
            char c = sortKeyString.charAt(0);
            if (c >= 97 && c <= 122) {
                return (char) (c - 32);
            }
            if (c >= 65 && c <= 90) {
                return c;
            }
        }
        return '#';
    }

    public static class SortKey {

        public String uuid;//customer uuid
        public String sortKeyString;
        public char sortKeyChar;

        public SortKey(String uuid, String sortKeyString, char sortKeyChar) {
            this.uuid = uuid;
            this.sortKeyString = sortKeyString;
            this.sortKeyChar = sortKeyChar;
        }
    }

    public static List<EntityCustomerAttachment> convertListAttaJsonToEntity(List<CustomerAtta> attas) {

        if (attas == null) {
            return null;
        }

        List<EntityCustomerAttachment> list = new ArrayList<>();

        for (CustomerAtta item : attas) {
            EntityCustomerAttachment entity = new EntityCustomerAttachment();
            entity.content = item.getContent();
            entity.customerUUId = item.getCustomerId();
            entity.fileName = item.getFileName();
            entity.fileType = item.getFileType();
            entity.path = item.getPath();
            entity.ServUrl = item.getServUrl();
            entity.remark = item.getRemark();
            entity.uuid = item.getAttachmentId();
            if (item.getLastUpdateTime() != null) {
                entity.lastUpdateTime = (int) DateUtils.parseTimestampInSecond(item.getLastUpdateTime());
            }
            list.add(entity);
        }
//        Collections.sort(list, CompareHelper.COMPARATOR_CUSTOMER_MODEL_NEW);
        return list;
    }

    public static CustomerAtta convertAttaEntityToJson(EntityCustomerAttachment entity) {
        if (entity == null) {
            return null;
        }
        CustomerAtta attaJson = new CustomerAtta();
        attaJson.setAttachmentId(entity.getUuid());
        attaJson.setPath(entity.getPath());
        attaJson.setContent(entity.getContent());
        attaJson.setCustomerId(entity.getCustomerUUId());
        attaJson.setFileName(entity.getFileName());
        attaJson.setFileType(entity.getFileType());
        attaJson.setHashKey(entity.getHashKey());
        attaJson.setRemark(entity.getRemark());
        attaJson.setServUrl(entity.getServUrl());
        return attaJson;
    }
}
